package com.cc.moxingvoice.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cc.moxingvoice.R;

/**
 * 创建日期：2018/3/3 on 22:36
 * 描述：
 * 作者：郭士超
 * QQ：1169380200
 */

public class ButtonTwoEditDialog extends Dialog implements View.OnClickListener {

    private EditText contentEdt;
    private TextView titleTxt;
    private TextView rightTxt;
    private TextView leftTxt;

    private Context mContext;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;
    private String hint;

    public ButtonTwoEditDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public ButtonTwoEditDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    public ButtonTwoEditDialog(Context context, int themeResId, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.listener = listener;
    }

    protected ButtonTwoEditDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public ButtonTwoEditDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public ButtonTwoEditDialog setPositiveButton(String name) {
        this.positiveName = name;
        return this;
    }

    public ButtonTwoEditDialog setNegativeButton(String name) {
        this.negativeName = name;
        return this;
    }

    public ButtonTwoEditDialog setHint(String hint) {
        this.hint = hint;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_button_two_edit);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        contentEdt = (EditText) findViewById(R.id.content);
        titleTxt = (TextView) findViewById(R.id.title);
        rightTxt = (TextView) findViewById(R.id.right);
        rightTxt.setOnClickListener(this);
        leftTxt = (TextView) findViewById(R.id.left);
        leftTxt.setOnClickListener(this);

        if (!TextUtils.isEmpty(positiveName)) {
            rightTxt.setText(positiveName);
        }

        if (!TextUtils.isEmpty(negativeName)) {
            leftTxt.setText(negativeName);
        }

        if (!TextUtils.isEmpty(title)) {
            titleTxt.setText(title);
        }

        if (!TextUtils.isEmpty(hint)) {
            contentEdt.setHint(hint);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left:
                if (listener != null) {
                    listener.onClick(this, false, "");
                }
                this.dismiss();
                break;
            case R.id.right:
                if (listener != null) {
                    String content = contentEdt.getText().toString().trim();
                    listener.onClick(this, true, content);
                }
                break;
        }
    }

    public interface OnCloseListener {
        void onClick(Dialog dialog, boolean confirm, String content);
    }

}
